package com.ttmall.product.service.impl;

import com.ttmall.product.entity.SkuImagesEntity;
import com.ttmall.product.entity.SpuInfoDescEntity;
import com.ttmall.product.service.AttrGroupService;
import com.ttmall.product.service.SkuImagesService;
import com.ttmall.product.service.SpuInfoDescService;
import com.ttmall.product.vo.SkuItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttmall.common.utils.PageUtils;
import com.ttmall.common.utils.Query;

import com.ttmall.product.dao.SkuInfoDao;
import com.ttmall.product.entity.SkuInfoEntity;
import com.ttmall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueServiceImpl saleAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.save(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        /**
         * key:
         * catelogId: 0
         * brandId: 0
         * min: 0
         * max: 0
         */
        String key = (String) params.get("key");

        if (StringUtils.isNotEmpty(key)){
            queryWrapper.and(w ->{
                  w.eq("sku_id",key).or().like("sku_name" , key);
            });
        }

        String catelogId = (String) params.get("catelogId");

        if (StringUtils.isNotEmpty(catelogId)&& !"0".equals(catelogId)){
            queryWrapper.eq("catalog_id" , catelogId);
        }

        String brandId = (String) params.get("brandId");

        if (StringUtils.isNotEmpty(brandId)&& !"0".equals(brandId)){
            queryWrapper.eq("brand_id" , brandId);
        }

        String min = (String) params.get("min");

        if (StringUtils.isNotEmpty(min)){
            queryWrapper.ge("price" , min);
        }

        String max = (String) params.get("max");

        if (StringUtils.isNotEmpty(max) && new BigDecimal(max).compareTo(new BigDecimal(0)) == 1){
            queryWrapper.le("price" , max);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {

        List<SkuInfoEntity> list = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));

        return list;
    }

    @Override
    public SkuItemVo item(Long skuId) {

        SkuItemVo skuItemVo = new SkuItemVo();
        //基本信息
        SkuInfoEntity info = getById(skuId);
        skuItemVo.setInfo(info);

        Long spuId = info.getSpuId();
        Long catalogId = info.getCatalogId();

        // 图片
        List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
        skuItemVo.setImages( images);

       List<SkuItemVo.SkuItemSaleAttrVo> saleAttrVos=  saleAttrValueService.getSaleAttrsBySpuId(spuId);
        skuItemVo.setSaleAttr(saleAttrVos);
        // 介绍
        SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(spuId);
        skuItemVo.setDesp(spuInfoDescEntity);

        // 规格参数
        List<SkuItemVo.SpuItemAttrGroupVo> attrGroupVos =  attrGroupService.getAttrGroupWithAttrsBySpuId(spuId,catalogId);

        skuItemVo.setGroupAttrs(attrGroupVos);

        return skuItemVo;
    }

}